package com.example.springbootdemo.controller;

import com.example.springbootdemo.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController("")
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    Config config;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("")
    public String index() {
        String r = String.format("redis v%s", config.getVersion());
        System.out.println(r);
        return r;
    }

    @GetMapping("add")
    public Object insert(@RequestParam("key") String key
            , @RequestParam("value") String value) {
        redisTemplate.opsForValue().set(key, value);
        Map<Object, Object> map = new HashMap<>();
        map.put(key, value);
        return map;
    }

    @GetMapping("get")
    public Object select(@RequestParam("key") String key) {

        String value = redisTemplate.opsForValue().get(key);
        Map<Object, Object> map = new HashMap<>();
        map.put(key, value);
        return map;
    }
}
